﻿using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UMLSynthesizer.CSharpAnalyzer.Helpers;
using UMLSynthesizer.LangAnalyzer.Expressions;
using UMLSynthesizer.LangAnalyzer.Statements;

namespace UMLSynthesizer.CSharpAnalyzer.Statements
{
    class CSharpSwitchStatement : CSharpStatementBase, ISwitchStatement
    {
        #region Fields

        IEnumerable<IStatementBase> _containedStatements;
        CSharpStatementBlockHelper blockHelper;
        CSharpExpressionHelper expressionHelper;

        #endregion

        #region Constructor

        public CSharpSwitchStatement(SwitchStatementSyntax statementNode, IStatementBase parentStatement)
            : base(statementNode, parentStatement, null, StatementKinds.Switch)
        {
            blockHelper = new CSharpStatementBlockHelper(this, null);
            expressionHelper = new CSharpExpressionHelper(null, this,
                () => statementNode.Expression);
        }

        #endregion

        #region Properties

        public IExpressionBase ControlExpression { get { return expressionHelper.Expression; } }
        public IEnumerable<IStatementBase> ContainedStatements
        {
            get
            {
                if (_containedStatements == null)
                    _containedStatements = new List<IStatementBase>(
                        (statementNode as SwitchStatementSyntax).Sections.SelectMany(
                        section =>
                        {
                            List<IStatementBase> statementList = new List<IStatementBase>();
                            foreach (var label in section.Labels)
                                if (label.Value != null)
                                    statementList.Add(new CSharpCaseLabel(label, this));
                                else
                                    statementList.Add(new CSharpDefaultLabel(label, this));
                            foreach (var statement in section.Statements)
                                statementList.Add(blockHelper.CreateCSharpStatement(statement));

                            return statementList;
                        }));
                return _containedStatements;
            }
        }

        #endregion
    }
}
